package com.app.business.service;

import com.app.business.model.*;
import com.app.system.jpa.JpaBaseDao;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by szy on 2024/9/21
 */
@SuppressWarnings(value = "ALL")
@Service
public class YxCarLotService extends JpaBaseDao<YxCarLot,Integer> {
    @Autowired
    private ContractService contractService;
    @Autowired
    private CarLotService carLotService;
    /**
     * 删除预选车位
     * @param carLotId
     * @param contractId
     */
    @Transactional
    public synchronized void deleteYxCatLot(Integer carLotId, Integer contractId) {
        String sql = "delete from yx_car_lot  where car_lot_id =:carLotId and contract_id =:contractId";
        this.entityManager.createNativeQuery(sql).setParameter("carLotId", carLotId).setParameter("contractId", contractId).executeUpdate();
    }

    /**
     * 预选车位List
     *
     * @param contractId
     * @return
     */
    public List<CarLot> getYxCarLotList(Integer contractId) {
        StringBuffer hql = new StringBuffer(" from CarLot cl  where cl.id ");
        hql.append(" in (select yx.carLotId from YxCarLot yx where yx.contractId =:contractId)");
        List<CarLot> carLotList = entityManager.createQuery(hql.toString()).setParameter("contractId", contractId).getResultList();
        return carLotList;
    }

    /**
     * 得到contractId对应下的carlotId
     *
     * @param contractId
     * @return
     */
    public List<Integer> getCatLotIdByContractId(Integer contractId) {
        Map<String, Object> map = Maps.newLinkedHashMap();
        StringBuffer sql = new StringBuffer("select yx.car_lot_id ");
        sql.append("from yx_car_lot yx  where yx.contract_id = :contractId  ");
        map.put("contractId", contractId);
        return (List<Integer>) this.findSqlObject(sql.toString(), map);
    }

    /**
     * 车位预选信息保存
     * @param request
     * @return
     * @throws IllegalAccessException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     */
    @Transactional
    public synchronized Response carLotYxSave(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Integer carLotId = ParamUtils.getInt(request, "carLotId", 0);

        CarLot carLot = this.carLotService.findOne(carLotId);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(carLot != null && contract != null, "1111", "数据错误！");

        List<Integer> yxCarLotIdList = this.getCatLotIdByContractId(contract.getId());
        if (!yxCarLotIdList.contains(carLotId) && carLotId != 0 && contract.getId() != 0){//该户不在预选中
            YxCarLot yxCarLot = new YxCarLot();
            yxCarLot.setCarLotId(carLotId);
            yxCarLot.setContractId(contract.getId());
            this.save(yxCarLot);
        }

        Map map = Maps.newHashMap();
        map.put("zsProjectId", contract.getZsProject().getId());
        map.put("azProjectId", carLot.getAzProject().getId());
        map.put("buildNum", carLot.getBuildNum());
        map.put("contractId", contract.getId());
        Response response = new Response();
        response.setData(map);
        response.setCode("0001");
        response.setMessage("保存成功！");
        return response;
    }
}
